<template>
  <div 
    :class="[
      'bx-progress',
      `bx-progress--${type}`,
      {
        [`bx-progress--${status}`]: status,
        [`bx-progress--${size}`]: size
      }
    ]"
  >
    <!-- 线形进度条 -->
    <template v-if="type === 'line'">
      <div class="bx-progress__outer">
        <div 
          class="bx-progress__inner bx-glass"
          :style="{ height: strokeWidth + 'px' }"
        >
          <div 
            class="bx-progress__bg"
            :style="progressStyle"
          >
            <div 
              v-if="showStripes"
              class="bx-progress__stripes"
            ></div>
          </div>
        </div>
        
        <div 
          v-if="showText"
          class="bx-progress__text"
        >
          <slot>{{ displayText }}</slot>
        </div>
      </div>
    </template>

    <!-- 环形进度条 -->
    <template v-else-if="type === 'circle'">
      <div 
        class="bx-progress__circle"
        :style="{ width: width + 'px', height: width + 'px' }"
      >
        <svg 
          :width="width" 
          :height="width"
          :viewBox="`0 0 ${width} ${width}`"
        >
          <!-- 背景圆环 -->
          <circle
            :cx="width / 2"
            :cy="width / 2"
            :r="radius"
            fill="none"
            :stroke="trailColor"
            :stroke-width="strokeWidth"
          />
          
          <!-- 进度圆环 -->
          <circle
            :cx="width / 2"
            :cy="width / 2"
            :r="radius"
            fill="none"
            :stroke="strokeColor"
            :stroke-width="strokeWidth"
            :stroke-dasharray="circumference"
            :stroke-dashoffset="strokeDashoffset"
            :stroke-linecap="strokeLinecap"
            class="bx-progress__circle-path"
            :style="circleStyle"
          />
        </svg>
        
        <div 
          v-if="showText"
          class="bx-progress__circle-text"
        >
          <slot>{{ displayText }}</slot>
        </div>
      </div>
    </template>

    <!-- 仪表盘进度条 -->
    <template v-else-if="type === 'dashboard'">
      <div 
        class="bx-progress__dashboard"
        :style="{ width: width + 'px', height: width * 0.75 + 'px' }"
      >
        <svg 
          :width="width" 
          :height="width * 0.75"
          :viewBox="`0 0 ${width} ${width * 0.75}`"
        >
          <!-- 背景弧线 -->
          <path
            :d="dashboardPath"
            fill="none"
            :stroke="trailColor"
            :stroke-width="strokeWidth"
          />
          
          <!-- 进度弧线 -->
          <path
            :d="dashboardPath"
            fill="none"
            :stroke="strokeColor"
            :stroke-width="strokeWidth"
            :stroke-dasharray="dashboardCircumference"
            :stroke-dashoffset="dashboardStrokeDashoffset"
            :stroke-linecap="strokeLinecap"
            class="bx-progress__dashboard-path"
            :style="circleStyle"
          />
        </svg>
        
        <div 
          v-if="showText"
          class="bx-progress__dashboard-text"
        >
          <slot>{{ displayText }}</slot>
        </div>
      </div>
    </template>
  </div>
</template>

<script>
import { computed } from 'vue'

export default {
  name: 'BxProgress',
  props: {
    percent: {
      type: Number,
      default: 0,
      validator: (value) => value >= 0 && value <= 100
    },
    type: {
      type: String,
      default: 'line',
      validator: (value) => ['line', 'circle', 'dashboard'].includes(value)
    },
    status: {
      type: String,
      default: '',
      validator: (value) => ['', 'success', 'exception', 'warning'].includes(value)
    },
    size: {
      type: String,
      default: 'medium',
      validator: (value) => ['small', 'medium', 'large'].includes(value)
    },
    strokeWidth: {
      type: Number,
      default: undefined
    },
    strokeColor: {
      type: [String, Object, Array],
      default: undefined
    },
    strokeLinecap: {
      type: String,
      default: 'round',
      validator: (value) => ['round', 'square'].includes(value)
    },
    trailColor: {
      type: String,
      default: undefined
    },
    width: {
      type: Number,
      default: 120
    },
    showText: {
      type: Boolean,
      default: true
    },
    format: {
      type: Function,
      default: undefined
    },
    showStripes: {
      type: Boolean,
      default: false
    }
  },
  setup(props) {
    const defaultStrokeWidth = computed(() => {
      if (props.strokeWidth !== undefined) return props.strokeWidth
      
      if (props.type === 'line') {
        return props.size === 'small' ? 6 : props.size === 'large' ? 10 : 8
      }
      return props.size === 'small' ? 4 : props.size === 'large' ? 8 : 6
    })

    const defaultStrokeColor = computed(() => {
      if (props.strokeColor !== undefined) return props.strokeColor
      
      switch (props.status) {
        case 'success':
          return 'rgba(52, 199, 89, 0.8)'
        case 'exception':
          return 'rgba(255, 59, 48, 0.8)'
        case 'warning':
          return 'rgba(255, 149, 0, 0.8)'
        default:
          return 'rgba(0, 122, 255, 0.8)'
      }
    })

    const defaultTrailColor = computed(() => {
      return props.trailColor || 'var(--bx-glass-border)'
    })

    const displayText = computed(() => {
      if (props.format) {
        return props.format(props.percent)
      }
      
      if (props.status === 'success') {
        return '✓'
      }
      
      if (props.status === 'exception') {
        return '✗'
      }
      
      return `${props.percent}%`
    })

    const progressStyle = computed(() => {
      const style = {
        width: `${props.percent}%`,
        backgroundColor: defaultStrokeColor.value
      }
      
      // 渐变色支持
      if (typeof props.strokeColor === 'object' && !Array.isArray(props.strokeColor)) {
        const { from, to } = props.strokeColor
        style.background = `linear-gradient(to right, ${from}, ${to})`
      } else if (Array.isArray(props.strokeColor)) {
        const colors = props.strokeColor.map((color, index) => {
          const percent = (index / (props.strokeColor.length - 1)) * 100
          return `${color} ${percent}%`
        }).join(', ')
        style.background = `linear-gradient(to right, ${colors})`
      }
      
      return style
    })

    // 圆形进度条相关计算
    const radius = computed(() => {
      return (props.width - defaultStrokeWidth.value) / 2
    })

    const circumference = computed(() => {
      return 2 * Math.PI * radius.value
    })

    const strokeDashoffset = computed(() => {
      return circumference.value * (1 - props.percent / 100)
    })

    const circleStyle = computed(() => {
      return {
        transition: 'stroke-dashoffset 0.3s ease'
      }
    })

    // 仪表盘进度条相关计算
    const dashboardPath = computed(() => {
      const centerX = props.width / 2
      const centerY = props.width / 2
      const startAngle = -225 * (Math.PI / 180) // -225度
      const endAngle = 45 * (Math.PI / 180) // 45度
      
      const startX = centerX + radius.value * Math.cos(startAngle)
      const startY = centerY + radius.value * Math.sin(startAngle)
      const endX = centerX + radius.value * Math.cos(endAngle)
      const endY = centerY + radius.value * Math.sin(endAngle)
      
      return `M ${startX} ${startY} A ${radius.value} ${radius.value} 0 1 1 ${endX} ${endY}`
    })

    const dashboardCircumference = computed(() => {
      return circumference.value * 0.75 // 270度弧线
    })

    const dashboardStrokeDashoffset = computed(() => {
      return dashboardCircumference.value * (1 - props.percent / 100)
    })

    return {
      defaultStrokeWidth,
      defaultStrokeColor,
      defaultTrailColor,
      displayText,
      progressStyle,
      radius,
      circumference,
      strokeDashoffset,
      circleStyle,
      dashboardPath,
      dashboardCircumference,
      dashboardStrokeDashoffset,
      strokeWidth: defaultStrokeWidth,
      strokeColor: defaultStrokeColor,
      trailColor: defaultTrailColor
    }
  }
}
</script>

<style scoped>
.bx-progress {
  font-family: var(--bx-font-family);
}

/* 线形进度条 */
.bx-progress--line .bx-progress__outer {
  display: flex;
  align-items: center;
  gap: var(--bx-spacing-sm);
}

.bx-progress__inner {
  flex: 1;
  background: var(--bx-glass-bg);
  backdrop-filter: var(--bx-glass-blur);
  border: 1px solid var(--bx-glass-border);
  border-radius: var(--bx-radius-medium);
  overflow: hidden;
  position: relative;
}

.bx-progress__bg {
  height: 100%;
  border-radius: inherit;
  transition: width var(--bx-transition-medium);
  position: relative;
  overflow: hidden;
}

.bx-progress__stripes {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-image: linear-gradient(
    45deg,
    rgba(255, 255, 255, 0.2) 25%,
    transparent 25%,
    transparent 50%,
    rgba(255, 255, 255, 0.2) 50%,
    rgba(255, 255, 255, 0.2) 75%,
    transparent 75%,
    transparent
  );
  background-size: 16px 16px;
  animation: bx-progress-stripes 1s linear infinite;
}

@keyframes bx-progress-stripes {
  0% {
    background-position: 0 0;
  }
  100% {
    background-position: 16px 0;
  }
}

.bx-progress__text {
  font-size: 14px;
  color: var(--bx-text-primary);
  white-space: nowrap;
  min-width: 40px;
  text-align: right;
}

.bx-progress--small .bx-progress__text {
  font-size: 12px;
  min-width: 32px;
}

.bx-progress--large .bx-progress__text {
  font-size: 16px;
  min-width: 48px;
}

/* 圆形进度条 */
.bx-progress__circle {
  position: relative;
  display: inline-block;
}

.bx-progress__circle-path {
  transform-origin: center;
  transform: rotate(-90deg);
}

.bx-progress__circle-text {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  font-size: 16px;
  font-weight: 600;
  color: var(--bx-text-primary);
  text-align: center;
}

.bx-progress--small .bx-progress__circle-text {
  font-size: 12px;
}

.bx-progress--large .bx-progress__circle-text {
  font-size: 20px;
}

/* 仪表盘进度条 */
.bx-progress__dashboard {
  position: relative;
  display: inline-block;
}

.bx-progress__dashboard-path {
  transform-origin: center;
}

.bx-progress__dashboard-text {
  position: absolute;
  top: 70%;
  left: 50%;
  transform: translate(-50%, -50%);
  font-size: 16px;
  font-weight: 600;
  color: var(--bx-text-primary);
  text-align: center;
}

.bx-progress--small .bx-progress__dashboard-text {
  font-size: 12px;
}

.bx-progress--large .bx-progress__dashboard-text {
  font-size: 20px;
}

/* 状态颜色 */
.bx-progress--success .bx-progress__text {
  color: rgba(52, 199, 89, 0.9);
}

.bx-progress--exception .bx-progress__text {
  color: rgba(255, 59, 48, 0.9);
}

.bx-progress--warning .bx-progress__text {
  color: rgba(255, 149, 0, 0.9);
}

/* 响应式设计 */
@media (max-width: 768px) {
  .bx-progress__text {
    font-size: 13px;
    min-width: 36px;
  }
  
  .bx-progress--small .bx-progress__text {
    font-size: 11px;
    min-width: 28px;
  }
  
  .bx-progress--large .bx-progress__text {
    font-size: 15px;
    min-width: 42px;
  }
  
  .bx-progress__circle-text,
  .bx-progress__dashboard-text {
    font-size: 14px;
  }
  
  .bx-progress--small .bx-progress__circle-text,
  .bx-progress--small .bx-progress__dashboard-text {
    font-size: 11px;
  }
  
  .bx-progress--large .bx-progress__circle-text,
  .bx-progress--large .bx-progress__dashboard-text {
    font-size: 18px;
  }
}
</style>

